package com.example.orderassembly.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.api.StoreApi;
import com.woniu.cloud.api.StoreGoodApi;
import com.woniu.cloud.api.StoreServiceMiddleApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TGood;
import com.woniu.cloud.entity.TStoreGood;
import com.woniu.cloud.entity.TStoreServiceMiddle;
import com.woniuxy.entity.ProductOrderDetail;
import com.woniuxy.entity.ServiceOrderDetail;
import com.example.orderassembly.mapper.ServiceOrderDetailMapper;
import com.example.orderassembly.service.IServiceOrderDetailService;
import com.woniuxy.form.InsertServiceOrderDetail;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务订单详情表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
@SuppressWarnings("all")
public class ServiceOrderDetailServiceImpl extends ServiceImpl<ServiceOrderDetailMapper, ServiceOrderDetail> implements IServiceOrderDetailService {
    @Resource
    ServiceOrderDetailMapper serviceOrderDetailMapper;
    @Resource
    StoreApi storeAPI;

    @Resource
    StoreGoodApi storeGoodApi;

    @Resource
    StoreServiceMiddleApi storeServiceMiddleApi;

    //todo  等待服务表查询
    @Override
    public Boolean insertServiceOrderDetail(InsertServiceOrderDetail insertServiceOrderDetail) {
        ServiceOrderDetail productOrderDetail = new ServiceOrderDetail();
        //todo 通过jwt 获取用户id
        Integer userId = insertServiceOrderDetail.getUserId();
        //设置订单id
        productOrderDetail.setServiceOrderDetailServiceOrderId(insertServiceOrderDetail.getServiceOrderId());
        //设置用户id
        productOrderDetail.setServiceOrderDetailUserId(userId);

        HashMap<Integer,Integer> map = insertServiceOrderDetail.getServiceAttribute();
        Set<Integer> set = map.keySet();
//        for (Integer key:set){
//            System.out.println(key);
//            System.out.println(map.get(key)+"==="+map.get(key).getClass());
//        }
//        Thread.sleep(20000);
        Integer num = 0;
        for (Integer key:set) {
            //设置数量
            productOrderDetail.setServiceOrderDetailNum(map.get(key));
            //根据id查询 服务商品信息
            Result goodByGoodId = storeServiceMiddleApi.getStoreServiceMiddleById(key);
            TStoreServiceMiddle data = JSON.parseObject(JSON.toJSONString(goodByGoodId.getData()), TStoreServiceMiddle.class);
            //设置价格
            productOrderDetail.setServiceOrderDetailPrice(new BigDecimal(data.getSecondServicePrice()));
            //设置小计
            productOrderDetail.setServiceOrderDetailTotal(new BigDecimal(data.getSecondServicePrice()*map.get(key)));
            //设置二级服务id
            productOrderDetail.setServiceOrderDetailSecondServiceId(data.getStoreServiceMiddleSecondServiceId());
            //设置名字
            productOrderDetail.setServiceOrderDetailServiceName(data.getSecondServiceName());
            //设置状态
            productOrderDetail.setServiceOrderDetailStatus(1);
            //插入数据库
            num += getBaseMapper().insert(productOrderDetail);
        }
        return set.size()==num;

    }

    @Override
    public List<ServiceOrderDetail> getServiceOrderDetailByProductOrderId(Integer serviceOrderId) {
        QueryWrapper<ServiceOrderDetail> productOrderDetailQueryWrapper = new QueryWrapper<>();
        productOrderDetailQueryWrapper.eq("service_order_detail_service_order_id",serviceOrderId);
        List<ServiceOrderDetail> productOrderDetails = serviceOrderDetailMapper.selectList(productOrderDetailQueryWrapper);
        return productOrderDetails;
    }
}
